package me.zhengjie.modules.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.management.domain.MrConferencePurpose;
import me.zhengjie.modules.management.domain.MrConferenceRoomEquips;
import me.zhengjie.modules.management.domain.MrMaterialDtl;
import me.zhengjie.modules.management.domain.Pageable;
import me.zhengjie.modules.management.mapper.MrConferencePurposeMapper;
import me.zhengjie.modules.management.mapper.MrMaterialDtlMapper;
import me.zhengjie.modules.management.service.MrConferencePurposeService;
import me.zhengjie.modules.management.service.dto.MrConferencePurposeCriteria;
import me.zhengjie.modules.management.service.dto.MrConferencePurposeDto;
import me.zhengjie.utils.PageHelpResultUtil;
import me.zhengjie.utils.PageInfoUtils;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;

import java.sql.Timestamp;
import java.util.*;

/**
 * @program: eladmin
 * @description: 会议室用途业务逻辑实现层
 * @author: ming.cao
 * @create: 2020-12-05 14:44
 **/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MrConferencePurposeServiceImpl implements MrConferencePurposeService {

    private final MrConferencePurposeMapper mrConferencePurposeMapper;

    private final MrMaterialDtlMapper mrMaterialDtlMapper;

    @Override
    public Map<String, Object> getAllList(MrConferencePurposeCriteria criteria, Pageable pageable) {
        PageHelper.startPage(pageable.getPage(),pageable.getSize());
        List<MrConferencePurpose> list = mrConferencePurposeMapper.getAllList(criteria);
        //分页数据
        PageInfo<MrConferencePurpose> pageInfo1 = new PageInfo<>(list);
        return PageHelpResultUtil.toPage(pageInfo1);
    }

    /**
     * 此功能未被调用（暂不符合需求）
     */
    @Override
    public Map<String, Object> getAllListForShow(MrConferencePurposeCriteria criteria, Pageable pageable) {

        List<MrConferencePurpose> list = mrConferencePurposeMapper.getAllList(criteria);
        List<MrConferencePurposeDto> listForShow = new ArrayList<>();
        for (MrConferencePurpose purpose : list) {
            MrConferencePurposeDto purposeDto = new MrConferencePurposeDto();
            //获取通过数量计算的材料ids数据
            String materialsByNo = purpose.getMaterialsByNo();
            String[] materialsByNoTempArray = null;

            //要用的通过数量计算的list
            List<Integer> materialsByNoList = new ArrayList();
            if (materialsByNo!=null && materialsByNo!=""){
                materialsByNoTempArray= materialsByNo.split(",");
                for (String s : materialsByNoTempArray) {
                    materialsByNoList.add(Integer.parseInt(s));
                }
            }

            if (materialsByNoList!=null){
                List<MrMaterialDtl> mrMaterialDtlList = new ArrayList<>() ;
                //遍历材料列表数据
                for (Integer materialId : materialsByNoList) {
                    mrMaterialDtlList.add(mrMaterialDtlMapper.getMrMaterialDtlById(materialId));
                }
                purposeDto.setMaterialsByNo(mrMaterialDtlList);
            }

            ///获取通过时间计算的材料id的数据
            String materialsByTime = purpose.getMaterialsByTime();
            String[] materialsByTimeTempArray = null;

            //要用的通过数量计算的list
            List<Integer> materialsByTimeList = new ArrayList();
            if (materialsByTime!=null && materialsByTime!=""){
                materialsByTimeTempArray= materialsByTime.split(",");
                for (String s : materialsByTimeTempArray) {
                    materialsByTimeList.add(Integer.parseInt(s));
                }
            }

            if (materialsByTimeList!=null){
                List<MrMaterialDtl> mrMaterialDtlList = new ArrayList<>() ;
                //遍历材料列表数据
                for (Integer materialId : materialsByNoList) {
                    mrMaterialDtlList.add(mrMaterialDtlMapper.getMrMaterialDtlById(materialId));
                }
                purposeDto.setMaterialsByTime(mrMaterialDtlList);

            }
            //purpose转成purposeDto
            purposeDto.setPurposeId(purpose.getPurposeId());
            purposeDto.setPurposeName(purpose.getPurposeName());
            purposeDto.setPurposeFor(purpose.getPurposeFor());
            purposeDto.setHadMaterial(purpose.getHadMaterial());
            purposeDto.setStatus(purpose.getStatus());
            purposeDto.setRemark(purpose.getRemark());
            purposeDto.setCreateDateTime(purpose.getCreateDateTime());
            purposeDto.setCreateUserId(purpose.getCreateUserId());
            purposeDto.setModifyDateTime(purpose.getModifyDateTime());
            purposeDto.setModifyUserId(purpose.getModifyUserId());

            listForShow.add(purposeDto);
        }

        return PageHelpResultUtil.toPage(PageInfoUtils.listToPageInfo(listForShow, pageable.getPage(), pageable.getSize()));
    }




    @Override
    public Map<String, Object> getMrConferencePurposeById(Integer id) {
        MrConferencePurpose purpose = mrConferencePurposeMapper.getMrConferencePurposeById(id);
       //要返回的数据
        MrConferencePurposeDto purposeDto = new MrConferencePurposeDto();

        //获取通过数量计算的材料ids数据
        String materialsByNo = purpose.getMaterialsByNo();
        String[] materialsByNoTempArray = null;

        //要用的通过数量计算的list
        List<Integer> materialsByNoList = new ArrayList();
        if (materialsByNo!=null && materialsByNo!=""){
            materialsByNoTempArray= materialsByNo.split(",");
            for (String s : materialsByNoTempArray) {
                materialsByNoList.add(Integer.parseInt(s));
            }
        }

        if (materialsByNoList!=null){
            List<MrMaterialDtl> mrMaterialDtlList = new ArrayList<>() ;
            //遍历材料列表数据
            for (Integer materialId : materialsByNoList) {
                mrMaterialDtlList.add(mrMaterialDtlMapper.getMrMaterialDtlById(materialId));
            }
            purposeDto.setMaterialsByNo(mrMaterialDtlList);
        }

        ///获取通过时间计算的材料id的数据
        String materialsByTime = purpose.getMaterialsByTime();
        String[] materialsByTimeTempArray = null;

        //要用的通过数量计算的list
        List<Integer> materialsByTimeList = new ArrayList();
        if (materialsByTime!=null && materialsByTime!=""){
            materialsByTimeTempArray= materialsByTime.split(",");
            for (String s : materialsByTimeTempArray) {
                materialsByTimeList.add(Integer.parseInt(s));
            }
        }

        if (materialsByTimeList!=null){
            List<MrMaterialDtl> mrMaterialDtlList = new ArrayList<>() ;
            //遍历材料列表数据
            for (Integer materialId : materialsByNoList) {
                mrMaterialDtlList.add(mrMaterialDtlMapper.getMrMaterialDtlById(materialId));
            }
            purposeDto.setMaterialsByTime(mrMaterialDtlList);

        }
        //purpose转成purposeDto
        purposeDto.setPurposeId(purpose.getPurposeId());
        purposeDto.setPurposeName(purpose.getPurposeName());
        purposeDto.setPurposeFor(purpose.getPurposeFor());
        purposeDto.setHadMaterial(purpose.getHadMaterial());
        purposeDto.setStatus(purpose.getStatus());
        purposeDto.setRemark(purpose.getRemark());
        purposeDto.setCreateDateTime(purpose.getCreateDateTime());
        purposeDto.setCreateUserId(purpose.getCreateUserId());
        purposeDto.setModifyDateTime(purpose.getModifyDateTime());
        purposeDto.setModifyUserId(purpose.getModifyUserId());

        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",purposeDto);
        return map;
    }

    @Override
    public Map<String, Object> getMrConferencePurposeById2(Integer id) {

        MrConferencePurpose purpose = mrConferencePurposeMapper.getMrConferencePurposeById(id);
        Map<String,Object> map = new LinkedHashMap<>(2);
        map.put("content",purpose);
        return map;
    }


    @Override
    public void addConferencePurpose(MrConferencePurposeDto mrConferencePurposeDto) {
        try{
            //按人数收费物资选项
            List<MrMaterialDtl>  mrMaterialDtlList1 =mrConferencePurposeDto.getMaterialsByNo();

            //按小时收费物资选项
            List<MrMaterialDtl>  mrMaterialDtlList2 =mrConferencePurposeDto.getMaterialsByTime();

            //将list拼接成string
            String materialsByNoStr1 = mrMaterialDtlToString(mrMaterialDtlList1);

            String materialsByTimeStr1 = mrMaterialDtlToString(mrMaterialDtlList2);

            MrConferencePurpose mrConferencePurpose = new MrConferencePurpose();
            //将purpose转成purposeDto
            mrConferencePurpose.setPurposeName(mrConferencePurposeDto.getPurposeName());
            mrConferencePurpose.setPurposeFor(mrConferencePurposeDto.getPurposeFor());
            mrConferencePurpose.setHadMaterial(mrConferencePurposeDto.getHadMaterial());

            //将string拼入数据
            mrConferencePurpose.setMaterialsByNo(materialsByNoStr1);
            mrConferencePurpose.setMaterialsByTime(materialsByTimeStr1);
            mrConferencePurpose.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrConferencePurpose.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrConferencePurposeMapper.addMrConferencePurpose(mrConferencePurpose);
        }catch(Exception e){
            throw new BadRequestException("新增失败");
        }

    }

    @Override
    public void addConferencePurpose2(MrConferencePurpose mrConferencePurpose) {
        try{
           //非空校验
            /*checkIsNull(mrConferencePurpose);*/
            //拼入数据
            mrConferencePurpose.setCreateDateTime(new Timestamp(new Date().getTime()));
            mrConferencePurpose.setCreateUserId(SecurityUtils.getCurrentUsername());
            mrConferencePurposeMapper.addMrConferencePurpose(mrConferencePurpose);
        }catch(Exception e){
            throw new BadRequestException("新增失败");
        }
    }


    @Override
    public void updateConferencePurpose(MrConferencePurposeDto mrConferencePurposeDto) {
        try{
            //按人数收费物资选项
            List<MrMaterialDtl>  mrMaterialDtlList1 =mrConferencePurposeDto.getMaterialsByNo();

            //按小时收费物资选项
            List<MrMaterialDtl>  mrMaterialDtlList2 =mrConferencePurposeDto.getMaterialsByTime();

            //将list拼接成string
            String materialsByNoStr1 = mrMaterialDtlToString(mrMaterialDtlList1);

            String materialsByTimeStr1 = mrMaterialDtlToString(mrMaterialDtlList2);
            MrConferencePurpose mrConferencePurpose = new MrConferencePurpose();
            //将purpose转成purposeDto
            mrConferencePurpose.setPurposeName(mrConferencePurposeDto.getPurposeName());
            mrConferencePurpose.setPurposeFor(mrConferencePurposeDto.getPurposeFor());
            mrConferencePurpose.setHadMaterial(mrConferencePurposeDto.getHadMaterial());
            //将string拼入数据
            mrConferencePurpose.setMaterialsByNo(materialsByNoStr1);
            mrConferencePurpose.setMaterialsByTime(materialsByTimeStr1);

            mrConferencePurpose.setModifyDateTime(new Timestamp(new Date().getTime()));
            mrConferencePurpose.setModifyUserId(SecurityUtils.getCurrentUsername());
            mrConferencePurposeMapper.updateMrConferencePurpose(mrConferencePurpose);
        }catch(Exception e){
            throw new BadRequestException("更新失败");
        }

    }

    @Override
    public void updateConferencePurpose2(MrConferencePurpose mrConferencePurpose) {
        try{
            //非空校验
           /* checkIsNull(mrConferencePurpose);*/
        mrConferencePurpose.setModifyDateTime(new Timestamp(new Date().getTime()));
        mrConferencePurpose.setModifyUserId(SecurityUtils.getCurrentUsername());

        mrConferencePurposeMapper.updateMrConferencePurpose(mrConferencePurpose);
        }catch (Exception e){
            throw new BadRequestException("更新失败");
        }
    }

    @Override
    @Transactional
    public void delConferencePurpose(List<Integer> ids) {
        try{
            for (Integer id : ids) {
                mrConferencePurposeMapper.delMrConferencePurpose(id);
            }
        }catch(Exception e){
            throw new BadRequestException("删除失败");
        }

    }

    //非空校验
    private void checkIsNull(MrConferencePurpose mrConferencePurpose){
        if (mrConferencePurpose.getPurposeName()!=null && mrConferencePurpose.getPurposeName()!=""){
            throw new BadRequestException("会议目的不能未空！");
        }
        if(mrConferencePurpose.getPurposeFor()!=null /*&& mrConferencePurpose.getPurposeName()!=""*/){
            throw new BadRequestException("用途对象不能为空！");
        }

    }

    //唯一性校验
    private void checkUnique(){

    }

    //拼接材料数据
    private String mrMaterialDtlToString( List<MrMaterialDtl>  mrMaterialDtlList ){

        //将list拼接成string
        StringBuilder materialsStr1 = new StringBuilder();
        if (mrMaterialDtlList!=null && mrMaterialDtlList.size()>0) {
            for (MrMaterialDtl dtl : mrMaterialDtlList) {
                materialsStr1.append(dtl.getMaterialId()+",");

            }
            if (materialsStr1.length()>0){
                materialsStr1.deleteCharAt(materialsStr1.length()-1);
            }
        }
        return materialsStr1.toString();
    }

}
